<?php

/**
 * Created by PhpStorm.
 * User: wanglin
 * Date: 2018/9/12
 * Time: 15:58
 */

namespace App\Console\Commands\ThirdOrder;

use App\Services\ThirdAPI\GameApiService;
use App\Services\ThirdAPI\MGAPIService;
use common\enum\CommonEnum;
use common\utils\RedisKeys;
use App\Services\UserService;
use Illuminate\Support\Facades\Redis;

class MGOrderProcessor extends BaseOrderProcessor
{
    protected $signature = self::PREFIX . 'mg';
    protected $description = 'MG注单拉取';

    public $gameNames;
    public $mgService;
    // 拉取注单的区间长度，比如从 4:00 - 5:00 一个小时的长度，根据注单量调整
    public $timeInterval = 10 * 60;
    // 自动重拉更新锁 redis key
    public $lockRedisKey = RedisKeys::MG_LOCK_AUTOFIX;
    // 拉取注单更新时间 redis key
    public $updateRedisKey = RedisKeys::MG_UPDATE_TIME;
    // 当前最新的拉取注单更新时间
    public $latestUpdateKey;

    /**
     * 初始化信息，子类需要重写这个方法
     */
    public function init($argv = null)
    {
        // 必须调用父类init，初始化公共信息
        parent::init();
        $this->mgService = new MGAPIService();
        // 5.第三方 partner id，名字，对应第三方表id
        $this->partnerName = "MG";
        $info = GameApiService::getApiPartnerInfoByName($this->partnerName);
        $this->partnerID = $info['id'];
        $this->orderType = self::ORDER_TYPE_EGAME;
        $this->reportOrderType = self::GAME_TYPE_EGAME;
        $this->gameNames = $this->mgService->getMGGameNames();
        $this->userInfos = [];
    }


    /**
     * 向第三方API抓取数据
     */
    function catchData()
    {
        $startTime = OrderStrategy::getUpdateKey($this->lockRedisKey, $this->updateRedisKey);
        $startTime = strtotime($startTime);
        // 拉取 $this->timeInterval 的数据，减10秒是防止出现临界数据
        $this->latestUpdateKey = $endTime = min(time(), $startTime + $this->timeInterval - 63);
        $data = $this->mgService->getMGOrders($startTime, $endTime);
        return $data;
    }

    /**
     * 刷新下次拉取的开始时间
     * @param $updateKey
     *
     */
    public function updateFlag($updateKey)
    {
        $redis = Redis::connection();
        $redis->set($this->updateRedisKey, date("Y-m-d H:i:s", $this->latestUpdateKey));
    }

    /**
     * 处理数据
     */
    public function load($data)
    {
        $this->betTime = $data['bet_time'];
        $this->settleTime = $data['settle_time'];
        $this->reportTime = $data['report_time'];
        $this->betMoney = $data['bet_money'];
        $this->prizeMoney = $data['prize_money'];
        $this->winLossMoney = $data['winloss_money'];
        $this->apiUserName = $data['api_username'];
        $this->contributeMoney = $data['contribute_money'];
        $this->bonusMoney = $data['bonus_money'];
        $this->validMoney = $data['valid_money'];
        $this->commonStatus = $data['common_status'];
        $this->updateKey = $data['update_key'];
        $this->apiStatus = $data['api_status'];
        $this->gameCode = $data['game_code'];
        $this->gameName = $data['game_name'];
        $this->betContent = $data['bet_content'];
        $this->originData = $data['origin_data'];
        $this->orderNumber = $data["order_number"];
    }


    /**
     * 处理数据，每个子类需要根据第三方的单条数据内容来获取有用的信息
     */
    function initFromData($data)
    {
        // TODO: Implement initFromData() method.
    }

    /**
     * 修复数据内容，每个子类需要实现怎么修复数据内容
     */
    function fixData($date)
    {
        // TODO: Implement fixData() method.
    }

    /**
     * 根据用户名解析出厅ID
     * prod_fg_80_1234
     * prodXfgX80X1234
     *
     */
    function getHallIDFromName($name)
    {
        if(strpos($name,'hong_') !==false){//兼容MG og的命名 ，他们自带固定的hong_字符
            $name=substr($name,5);
        }
        $splits = ['_', 'X'];
        foreach ($splits as $s) {
            $arr = explode($s, strtoupper($name));
            if (count($arr) > 3) {
                return $arr[2];
            }
        }
        return 80;
    }


}